package com.kingcore.director.core.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import wzw.util.StringUtils;

import com.kingcore.director.main.MainFrame;

public class XMLUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		executeQuery("select TITLE,INTERVAL,USEFUL from AWOKE_MAIN");

	}
	
	public static List executeQuery(String sql){

		// 解析信息
		String str_table="";
		String[] str2_cols ;
		sql=sql.toUpperCase();
		str_table = sql.substring(sql.indexOf("FROM")+4).trim();
		str2_cols = sql.substring(sql.indexOf("SELECT")+6, sql.indexOf("FROM") ).split(",");

		//System.out.println(str_table);
		//for (int i = 0; i < str2_cols.length; i++) {
		//	System.out.println(str2_cols[i].trim() );
	//		
		//}
		
		// 解析文件
		List rs=new ArrayList();
       SAXBuilder sb = new SAXBuilder();
       try {
		Document doc = sb.build( System.getProperty(MainFrame.User_Data_Path) + str_table +".xml");
	     Element root = doc.getRootElement();
	      List rowList = root.getChildren("ROW");
	      Element rowElement ;
	      for (int i = 0; i < rowList.size(); i++) {
	    	  rowElement =(Element) rowList.get(i);
	    	  Hashtable row = new Hashtable();
	    	  for (int j = 0; j < str2_cols.length; j++) {
	    		  //System.out.println("put="+str2_cols[j].trim()+"  "+ StringUtils.toGB2312( rowElement.getChildTextTrim( str2_cols[j].trim() )) );
	    		  row.put(str2_cols[j].trim(), rowElement.getChildTextTrim( StringUtils.toGB2312( str2_cols[j].trim()) ) );
			}
	    	  rs.add(row);
		}
	      
	} catch (JDOMException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

		return rs;
	}

}
